<?php
/**
 * series controller
 * @package app-drama_controller
 */
load_controller('/base');
/**
 * series controller
 * @author pandajingjing
 * @package app-drama_controller
 */
class seriescontroller extends basecontroller{

    function doRequest(){
        $this->setModule('series');
        $strURLAction=$this->getParam('action','url');
        $this->setData('strURLAction',$strURLAction);
        switch($strURLAction){
            case 'add':
                return $this->doAdd();
                break;
            case 'edit':
                return $this->doEdit();
                break;
            case 'list':
            default:
                return $this->doList();
                break;
        }
    }

    /**
	 * 获取访问该控制器的路径
	 */
    static function getURL($p_strAction='',$p_arrParam=array(),$p_bolSecure=false,$p_strPre=''){
        return parent::getURL('/series',$p_strAction,$p_arrParam);
    }

    /**
	 * 执行列表功能
	 * @return string
	 */
    function doList(){
        load_lib('/bll/series');
        $clsBll=new bll_series();
        $arrTypeList=$clsBll->getTypeList();
        $arrClassList=$clsBll->getClassList();
        $arrRatioList=$clsBll->getRatioList();
        $intTypeID=$this->getParam('t','url','int');
        $intClassID=$this->getParam('c','url','int');
        $intRatioID=$this->getParam('r','url','int');
        foreach($arrTypeList as $intIndex=>$arrType){
            $arrTypeList[$intIndex]['URL']=self::getURL('list',array( 
                    't'=>$arrType['TYPEID'],
                    'c'=>$intClassID,
                    'r'=>$intRatioID 
            ));
            if($intTypeID==$arrType['TYPEID']){
                $arrTypeList[$intIndex]['SELECTED']=true;
            }else{
                $arrTypeList[$intIndex]['SELECTED']=false;
            }
        }
        foreach($arrClassList as $intIndex=>$arrClass){
            $arrClassList[$intIndex]['URL']=self::getURL('list',array( 
                    't'=>$intTypeID,
                    'r'=>$intRatioID,
                    'c'=>$arrClass['CLASSID'] 
            ));
            if($intClassID==$arrClass['CLASSID']){
                $arrClassList[$intIndex]['SELECTED']=true;
            }else{
                $arrClassList[$intIndex]['SELECTED']=false;
            }
        }
        foreach($arrRatioList as $intIndex=>$arrRatio){
            $arrRatioList[$intIndex]['URL']=self::getURL('list',array( 
                    't'=>$intTypeID,
                    'r'=>$arrRatio['RATIOID'],
                    'c'=>$intClassID 
            ));
            if($intRatioID==$arrRatio['RATIOID']){
                $arrRatioList[$intIndex]['SELECTED']=true;
            }else{
                $arrRatioList[$intIndex]['SELECTED']=false;
            }
        }
        $intPage=$this->getParam('p','url','int');
        $arrSeriesList=$clsBll->searchSeries(array( 
                'CLASSID'=>$intClassID,
                'RATIOID'=>$intRatioID,
                'TYPEID'=>$intTypeID,
                'KEYWORD'=>'' 
        ),$intPage);
        $arrSeriesList['PAGE']=$this->setPage($arrSeriesList['PAGE'],'list','p','/series',array( 
                'c'=>$intClassID,
                't'=>$intTypeID,
                'r'=>$intRatioID 
        ));
        load_controller('/episode');
        foreach($arrSeriesList['DATA'] as $intIndex=>$arrSeries){
            $arrSeriesList['DATA'][$intIndex]['EPURL']=episodecontroller::getURL('detail',array( 
                    'sid'=>$arrSeries['SERID'] 
            ));
        }
        $this->setData('arrSeriesList',$arrSeriesList);
        $this->setData('strTypeURL',self::getURL('list',array( 
                'r'=>$intRatioID,
                'c'=>$intClassID 
        )));
        $this->setData('strClassURL',self::getURL('list',array( 
                'r'=>$intRatioID,
                't'=>$intTypeID 
        )));
        $this->setData('strRatioURL',self::getURL('list',array( 
                't'=>$intTypeID,
                'c'=>$intClassID 
        )));
        $this->setData('strAddSeriesURL',self::getURL('add'));
        $this->setData('arrTypeList',$arrTypeList);
        $this->setData('arrClassList',$arrClassList);
        $this->setData('arrRatioList',$arrRatioList);
        return '/series/list';
    }

    /**
	 * 添加节目
	 * @return string
	 */
    function doAdd(){
        $arrErrInfo=array();
        $strPostAction=$this->getParam('hidAction','post');
        load_lib('/bll/series');
        $clsBll=new bll_series();
        $arrTypeList=$clsBll->getTypeList();
        $arrClassList=$clsBll->getClassList();
        $arrRatioList=$clsBll->getRatioList();
        
        if('saveadd'==$strPostAction){
            $txtCName=$this->getParam('txtCName','post');
            $txtEName=$this->getParam('txtEName','post');
            $dropRatio=$this->getParam('dropRatio','post');
            $dropType=$this->getParam('dropType','post');
            $dropClass=$this->getParam('dropClass','post');
            $mixResult=$clsBll->addSeries($txtCName,$txtEName,$dropClass,$dropType,$dropRatio);
            if(is_array($mixResult)){
                $arrErrInfo=$mixResult;
            }else{
                $this->redirectURL(self::getURL());
            }
        }else{
            $this->setViewState('frmEditSeries','txtCName','');
            $this->setViewState('frmEditSeries','txtEName','');
            $this->setViewState('frmEditSeries','dropRatio',0);
            $this->setViewState('frmEditSeries','dropType',0);
            $this->setViewState('frmEditSeries','dropClass',0);
            $this->setViewState('frmEditSeries','hidAction','saveadd');
            $this->setViewState('frmEditSeries','chkSaw',0);
        }
        $this->setData('arrTypeList',$arrTypeList);
        $this->setData('arrClassList',$arrClassList);
        $this->setData('arrRatioList',$arrRatioList);
        $this->setData('arrErrInfo',$arrErrInfo);
        return '/series/edit';
    }

    /**
	 * 编辑节目
	 * @return error_404controller|string
	 */
    function doEdit(){
        $arrErrInfo=array();
        $strPostAction=$this->getParam('hidAction','post');
        load_lib('/bll/series');
        $clsBll=new bll_series();
        $arrTypeList=$clsBll->getTypeList();
        $arrClassList=$clsBll->getClassList();
        $arrRatioList=$clsBll->getRatioList();
        
        if('saveedit'==$strPostAction){
            $txtCName=$this->getParam('txtCName','post');
            $txtEName=$this->getParam('txtEName','post');
            $dropRatio=$this->getParam('dropRatio','post');
            $dropType=$this->getParam('dropType','post');
            $dropClass=$this->getParam('dropClass','post');
            $chkSaw=$this->getParam('chkSaw','post');
            $intSerID=$this->getParam('sid','url','int');
            $mixResult=$clsBll->saveSeries($intSerID,$txtCName,$txtEName,$dropClass,$dropType,$dropRatio,$chkSaw);
            if(is_array($mixResult)){
                $arrErrInfo=$mixResult;
            }else{
                load_controller('/episode');
                $this->redirectURL(episodecontroller::getURL('detail',array( 
                        'sid'=>$intSerID 
                )));
            }
        }else{
            $intSerID=$this->getParam('sid','url','int');
            if(null===$intSerID){
                load_controller('/error/404');
                return new error_404controller();
            }
            $arrSeries=$clsBll->getSeriesDetail($intSerID);
            if(null===$arrSeries){
                load_controller('/error/404');
                return new error_404controller();
            }
            $this->setViewState('frmEditSeries','txtCName',$arrSeries['CNAME']);
            $this->setViewState('frmEditSeries','txtEName',$arrSeries['ENAME']);
            $this->setViewState('frmEditSeries','dropRatio',$arrSeries['RATIOID']);
            $this->setViewState('frmEditSeries','dropType',$arrSeries['TYPEID']);
            $this->setViewState('frmEditSeries','dropClass',$arrSeries['CLASSID']);
            $this->setViewState('frmEditSeries','hidAction','saveedit');
            $this->setViewState('frmEditSeries','chkSaw',$arrSeries['SAW']);
        }
        $this->setData('arrTypeList',$arrTypeList);
        $this->setData('arrClassList',$arrClassList);
        $this->setData('arrRatioList',$arrRatioList);
        $this->setData('arrErrInfo',$arrErrInfo);
        return '/series/edit';
    }
}